var longestConsecutive = function(nums) {
  const numSet = new Set(nums);
  let ans = 0;

  for (const num of numSet) {
    // 如果num-1不存在，说明num就是一个连续序列的起点
    if(!numSet.has(num - 1)) {
      let currentNum = num, currentLen = 1;

      while(numSet.has(currentNum + 1)) {
        currentNum++;
        currentLen++;
      }

      ans = Math.max(ans, currentLen);
    }
  }
  return ans;
};

const nums = [100,4,200,1,3,2,2];
console.log(longestConsecutive(nums))